package com.coder.oj.common.enums;

import java.util.*;

/**
 * 请求方式枚举
 * @author Bosen 2023/1/1 21:59
 */
public enum RestEnum {

    UNKNOWN("", ""),
    GET("GET", "GET"),
    POST("POST", "POST"),
    PUT("PUT", "PUT"),
    DELETE("DELETE", "DELETE"),
    ;

    /**
     * 名称
     */
    private String name;

    /**
     * 值
     */
    private String value;

    RestEnum(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 通过值获取枚举 
     * @author Bosen 2022/11/23 14:42
     **/
    public static RestEnum getEnum(Integer value){
        RestEnum resultEnum = null;
        RestEnum[] enumAry = RestEnum.values();
        for(RestEnum commonStatusEnum : enumAry) {
            if (commonStatusEnum.getValue().equals(value)) {
                resultEnum = commonStatusEnum;
                break;
            }
        }
        resultEnum = RestEnum.UNKNOWN;
        return resultEnum;
    }

    /**
     * 所有枚举转为map 
     * @author Bosen 2022/11/23 14:42
     **/
    public static Map<String, Map<String, Object>> toMap() {
        RestEnum[] ary = RestEnum.values();
        Map<String, Map<String, Object>> enumMap = new HashMap<>();
        for(RestEnum commonStatusEnum : ary) {
            if (!Objects.equals(commonStatusEnum.getValue(), RestEnum.UNKNOWN.getValue())) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", commonStatusEnum.getName());
                map.put("value", String.valueOf(commonStatusEnum.getValue()));
                enumMap.put(commonStatusEnum.getValue(), map);
            }
        }
        return enumMap;
    }

    /**
     * 所有枚举转为list
     * @author Bosen 2022/11/23 14:43
     **/
    public static List<Map<String,String>> toList(){
        RestEnum[] ary = RestEnum.values();
        List<Map<String,String>> list = new ArrayList<>();
        for (RestEnum commonStatusEnum : ary) {
            if (!Objects.equals(commonStatusEnum.getValue(), RestEnum.UNKNOWN.getValue())) {
                Map<String, String> map = new HashMap<>();
                map.put("name", commonStatusEnum.getName());
                map.put("value", String.valueOf(commonStatusEnum.getValue()));
                list.add(map);
            }
        }
        return list;
    }
}
